﻿Imports System.ComponentModel
Imports System.Text.RegularExpressions

Public Class SampleInputResultsWindow
    Implements INotifyPropertyChanged

#Region "Events"
    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
#End Region

#Region "Properties"

    Private _regexPattern As String
    Public Property RegexPattern() As String
        Get
            Return _regexPattern
        End Get
        Set(ByVal value As String)
            _regexPattern = value
            OnPropertyChanged(New PropertyChangedEventArgs("RegexPattern"))
        End Set
    End Property

    Private _sampleInput As String
    Public Property SampleInput() As String
        Get
            Return _sampleInput
        End Get
        Set(ByVal value As String)
            _sampleInput = value
            OnPropertyChanged(New PropertyChangedEventArgs("SampleInput"))
        End Set
    End Property

    Private _regexOptions As RegexOptions
    Public Property RegexOptions() As RegexOptions
        Get
            Return _regexOptions
        End Get
        Set(ByVal value As RegexOptions)
            _regexOptions = value
            OnPropertyChanged(New PropertyChangedEventArgs("RegexOptions"))
        End Set
    End Property

    Private _matches As IEnumerable(Of MatchResult)
    Public Property Matches() As IEnumerable(Of MatchResult)
        Get
            Return _matches
        End Get
        Set(ByVal value As IEnumerable(Of MatchResult))
            _matches = value
            OnPropertyChanged(New PropertyChangedEventArgs("Matches"))
        End Set
    End Property

    Private _selectedItem As Object
    Public Property SelectedItem() As Object
        Get
            Return _selectedItem
        End Get
        Set(ByVal value As Object)
            _selectedItem = value
            OnPropertyChanged(New PropertyChangedEventArgs("SelectedItem"))
        End Set
    End Property

    Private _selectionStart As Integer
    Public Property SelectionStart() As Integer
        Get
            Return _selectionStart
        End Get
        Set(ByVal value As Integer)
            _selectionStart = value
            OnPropertyChanged(New PropertyChangedEventArgs("SelectionStart"))
        End Set
    End Property

    Private _selectionLength As Integer
    Public Property SelectionLength() As Integer
        Get
            Return _selectionLength
        End Get
        Set(ByVal value As Integer)
            _selectionLength = value
            OnPropertyChanged(New PropertyChangedEventArgs("SelectionLength"))
        End Set
    End Property

#End Region

#Region "Constructors"

#End Region

#Region "Methods"
    Private Sub LoadMatches()
        Try
            Dim l_matches = Regex.Matches(Me.SampleInput, Me.RegexPattern, Me.RegexOptions)
            Me.Matches =
                From m In l_matches.OfType(Of Match)()
                Select New With {
                    .Match = m,
                    .Details = New Object() {
                        New With {.Title = "Captures", .Items = m.Captures},
                        New With {.TItle = "Groups", .Items = m.Groups}
                    }
                }

            'Dim m As Match
            'Dim c As Capture
            'Dim g As Group
        Catch ex As ArgumentException

        End Try
    End Sub
#End Region

#Region "Event Raiser Methods"
    Protected Overloads Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
        RaiseEvent PropertyChanged(sender:=Me, e:=e)
    End Sub
#End Region

#Region "Event Handlers"
    Private Sub SampleInputResultsWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        LoadMatches()
    End Sub

    Private Sub SampleInputResultsWindow_PropertyChanged(ByVal sender As Object, ByVal e As PropertyChangedEventArgs) Handles Me.PropertyChanged
        Select Case e.PropertyName

            Case "SelectedItem"
                If TypeOf Me.SelectedItem Is Match Then
                    Dim l_match = CType(Me.SelectedItem, Match)
                    Me.SelectionStart = l_match.Index
                    Me.SelectionLength = l_match.Length
                ElseIf TypeOf Me.SelectedItem Is Capture Then
                    Dim l_capture = CType(Me.SelectedItem, Capture)
                    Me.SelectionStart = l_capture.Index
                    Me.SelectionLength = l_capture.Length
                ElseIf TypeOf Me.SelectedItem Is Group Then
                    Dim l_group = CType(Me.SelectedItem, Group)
                    Me.SelectionStart = l_group.Index
                    Me.SelectionLength = l_group.Length
                End If

            Case "SelectionStart"
                SampleInputTextBox.SelectionStart = Me.SelectionStart

            Case "SelectionLength"
                SampleInputTextBox.SelectionLength = Me.SelectionLength

        End Select
    End Sub

    Private Sub MatchesTreeView_SelectedItemChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedPropertyChangedEventArgs(Of System.Object))
        Me.SelectedItem = e.NewValue
    End Sub
#End Region

    'Public Class MatchResult

    '    Private _match As Match
    '    Public Property Match() As Match
    '        Get
    '            Return _match
    '        End Get
    '        Private Set(ByVal value As Match)
    '            _match = value
    '        End Set
    '    End Property

    '    Private _details As IEnumerable(Of MatchResultDetail)
    '    Public Property Details As IEnumerable(Of MatchResultDetail)
    '        Get
    '            Return _details
    '        End Get
    '        Private Set(ByVal value As IEnumerable(Of MatchResultDetail))
    '            _details = value
    '        End Set
    '    End Property

    '    Friend Sub New(ByVal match As Match)
    '        Me.Match = match
    '        Me.Details = New MatchResultDetail() {
    '            New MatchResultDetail("Captures", Me.Match.Captures),
    '            New MatchResultDetail("Groups", Me.Match.Groups)
    '        }
    '    End Sub

    'End Class

    'Public Class MatchResultDetail

    '    Private _title As String
    '    Public Property Title() As String
    '        Get
    '            Return _title
    '        End Get
    '        Private Set(ByVal value As String)
    '            _title = value
    '        End Set
    '    End Property

    '    Private _items As IEnumerable
    '    Public Property Items() As IEnumerable
    '        Get
    '            Return _items
    '        End Get
    '        Private Set(ByVal value As IEnumerable)
    '            _items = value
    '        End Set
    '    End Property

    '    Friend Sub New(ByVal title As String, ByVal items As IEnumerable)
    '        Me.Title = title
    '        Me.Items = items
    '    End Sub

    'End Class

End Class



#Region "Command Classes"
Partial Class SampleInputResultsWindow


#Region ""

#End Region


End Class
#End Region